import * as lodashFind from "lodash.find"
import * as lodashEmpty from "lodash.isempty"
export {default as lodashAssign} from 'lodash.assign';
export {default as lodashGet} from 'lodash.get';
export {default as lodashGroupBy} from 'lodash.groupby';
export {default as lodashMap} from 'lodash.map';
export {default as lodashPick} from 'lodash.pick';
export {default as lodashKeys} from 'lodash.keys';
export {default as lodashFill} from 'lodash.fill';
export {default as lodashFind} from 'lodash.find';
export {default as lodashDifference} from 'lodash.difference';
export {default as lodashDifferenceBy} from 'lodash.differenceby';
export {default as lodashDetaultto} from 'lodash.defaultto';
export {default as lodashreduce} from 'lodash.reduce';
export {default as lodashUnion} from 'lodash.union';
export {default as lodashUnionBy} from 'lodash.unionby';

export {default as lodashEmpty} from 'lodash.isempty';

export {default as lodashTimes} from 'lodash.times';

export function getPropWithDefault(obj, keypath, defaultValue) {
  return lodashGet(obj, keypath, defaultValue);
}

function clearObjectValue(obj) {
  var keys = Object.keys(obj);
  for (let i = 0; i < keys.length; i++) {
    var key = keys[i];
    if (typeof obj[key] === 'string') {
      obj[key] = '';
    }
    if (typeof obj[key] === 'number') {
      obj[key] = 0;
    }
    
  }
}

export function lodashFillData(mainobj = {}, data = {}) {
  var keys = Object.keys(mainobj);
  for (let i = 0; i < keys.length; i++) {
    var key = keys[i];
    if (typeof mainobj[key] == 'object') {
      if (lodashEmpty(data[key])) {
        clearObjectValue(mainobj[key]);
      } else {
        mainobj[key] = data[key];
      }
    } else {
      mainobj[key] = data[key];
    }
    
  }
}


export function pickProps(source, keys = []) {
  return lodashPick(source, keys);
}

export function findOneInArray(array = [], cb) {
  var one = lodashFind(array, cb) || {};
  return one;
}


export function lodashExit(array = [], cb) {
  var one = lodashFind(array, cb) || {};
  return !lodashEmpty(one);
}

/**
 * @description: 判断是不是数字o
 * @params
 * @return
 */
export function isZeroNumber(number) {
  if (number == '0') return true;
}


//from elementui
export const isEmpty = function (val) {
  // null or undefined
  if (val == null) return true;
  
  if (typeof val === 'boolean') return false;
  
  if (typeof val === 'number') return !val;
  
  if (val instanceof Error) return val.message === '';
  
  switch (Object.prototype.toString.call(val)) {
    // String or Array
    case '[object String]':
    case '[object Array]':
      return !val.length;
    
    // Map or Set or File
    case '[object File]':
    case '[object Map]':
    case '[object Set]': {
      return !val.size;
    }
    // Plain Object
    case '[object Object]': {
      return !Object.keys(val).length;
    }
  }
  
  return false;
};

/**
 * @description: 交集   a b 是一个数组
 * @params
 * @return
 */
export function intersectionArray(a = [], b = []) {
  let intersection = a.filter(v => b.includes(v));
  return intersection;
}

//合并2个数组 并且去重
export function merrayArrayDuplicate(a = [], b = []) {
  var map = {};
  for (let i = 0; i < b.length; i++) {
    if (b[i]) {
      map[b[i]] = b[i];
    }
    
  }
  for (let i = 0; i < a.length; i++) {
    if (a[i]) {
      map[a[i]] = a[i];
    }
  }
  return lodashMap(map, v => v);
}

/**
 * @description:
 * @params
 * @return  boolean
 */
export function isExit(array = [], func) {
  var one = findOneInArray(array, func);
  return !lodashEmpty(one);
}
